import { PageVo, R, Teacher, TeacherQueryVo } from "./model";
import request from "../utils/request"
import { ElMessage } from "element-plus";

/**
 * 
 * @returns 查询数据
 */
export const listAll = () => {
    return request<R<PageVo<Teacher>>>({
        url: '/api/teacher',
        method: "GET"
    })
        .then((response) => {
            return response;
        })
        .catch((error) => {
            return Promise.reject(error);
        })
}
// 注意：在自定义API中直接返回实际数据response.data。

export const listPage = (currentPage: number, pageSize: number, teacherQueryVo?: TeacherQueryVo) => {
    // "list?page=&size=&name=..." 
    return request<R<PageVo<Teacher>>>({
        url: "/api/teacher",
        method: "GET",
        // URL参数 ?key=value& 
        params: {
            currentPage: currentPage,
            pageSize: pageSize,
            ...teacherQueryVo // 对象拓展运算符
        }
    })
        .then((response) => {
            // response.data.data: PageVo<Teacher> 
            return response;
        });
}

export const getById = (id: string) => {
    // ES6模板字符串 
    let uri = `/api/teacher/${id}`;
    // function axios(config: AxiosRequest) { 
    return request<R<Teacher>>({
        url: uri,
        method: 'GET'
    })
        .then((response) => {
            return response;
        });
}

export const save = (teacher: Teacher) => {
    return request<R<string>>({
        url: "/api/teacher",
        method: 'POST',
        data: teacher
    })
        .then((response) => {
            // response.data.data: string 
            return response;
        });
}



export const update = (teacher: Teacher) => {
    return request<R<string>>({
        url: "/api/teacher",
        method: 'PUT',
        data: teacher
    })
        .then((response) => {
            return response;
        });
}

export const remove = (id: string) => {
    // ES6模板字符串
    let uri = `/api/teacher/${id}`;
    return request<R<string>>({
        url: uri,
        method: 'DELETE'
    })
        .then((response) => {
            return response;
        });
}
export function uploadFile(files: File[]) {
    // 创建一个表单数据对象
    const formData = new FormData();

    files.forEach((file) => {
        formData.append("file", file);
    });

    return request<R<string>>({
        url: "/api/file/upload",
        method: "POST",
        // 上传文件，必须指定媒体类型为 multipart/form-data
        headers: { "Content-Type": "multipart/form-data" },
        data: formData,
    }).then((response) => {
        // response.data.data: string 即头像路径
        return response.data;
    });
}